﻿using Api.Common;
using Api.Model.Dto.ArticleDto;
using Api.Model.Model;
using Api.StartUpConfig;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.AutoMapper.ArticleResolver
{
    public class CreateArticleUrlResolver: IValueResolver<CreateArticleInput, Article, string>
    {
        public string Resolve(CreateArticleInput source, Article destination, string destMember, ResolutionContext context)
        {
            var dir = StaticInfo.WebRootPath + "/Article/";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var url = dir + Guid.NewGuid() + ".md";
            File.WriteAllText(url, source.Body);
            return url.Substring(url.IndexOf("/Article/"));
        }
    }
}
